
var path="http://cdnui.nat100.top/hzf-wxplatform-web/";

//添加到购物车
function add(id){

        $('#count'+id).val(parseInt($('#count'+id).val())+parseInt(1));
        if($('#'+id).attr('data')==1){
            var count = $('#count'+id).val();
            var price = $('#price'+id).attr('data');
            var installprice = $('#installprice'+id).attr('data');
            var allinstallPrice = parseInt(count)*parseFloat(installprice);
            var zongPrice = $('#zonge').val();
            var allPrice=parseFloat(price)+parseFloat(zongPrice)+parseFloat(allinstallPrice);
            $('#zonge').val(toDecimal2(allPrice));
            $('.heqian').html("￥："+toDecimal2(allPrice));

        }
}
function minus(id){
    if($('#count'+id).val()>0){
    $('#count'+id).val(parseInt($('#count'+id).val())-parseInt(1));

    if($('#'+id).attr('data')==1){
        var count = $('#count'+id).val();
        var price = $('#price'+id).attr('data');
        var installprice = $('#installprice'+id).attr('data');
        var allinstallPrice = parseInt(count)*parseFloat(installprice);
        var zongPrice = $('#zonge').val();
        var allPrice=parseFloat(zongPrice)-parseFloat(price)-parseFloat(allinstallPrice);
        $('#zonge').val(toDecimal2(allPrice));
        $('.heqian').html("￥："+toDecimal2(allPrice));
    }}else{
    alert("最小为0")
}
}
$('.quantu').click(function(){
    if($(this).attr('data')==0){
        this.setAttribute("data",1);

        $(this).css({
        background: "url("+path+"/images/hongdui.png) no-repeat center",
        backgroundSize:'contain'
    });
    $('#zonge').val(0);
    $(".xuwance").each(function(){
        this.setAttribute("data",1);

        var id = $(this).attr('id');
        var count = $('#count'+id).val();
        var price = $('#price'+id).attr('data');
        var installprice = $('#installprice'+id).attr('data');
        var allinstallPrice = parseInt(count)*parseFloat(installprice);
        var zongPrice = $('#zonge').val();
        var allPrice=parseFloat(zongPrice)+parseFloat(allinstallPrice)+parseFloat(price)*parseInt(count);
        $('#zonge').val(toDecimal2(allPrice));
        $('.heqian').html("￥："+toDecimal2(allPrice));
        $('.xuwance').css({
            background: "url("+path+"/images/hongdui.png)",
            backgroundSize:'contain'
        });
})
}else{
        $(this).css({
            background: "url("+path+"/images/raw_1503991404.png)",
            backgroundSize:'contain'
        });
        $('.xuwance').css({
            background: "url("+path+"/images/raw_1503991404.png)",
            backgroundSize:'contain'
        });
        $('#zonge').val(0);
        $('.heqian').html("￥："+toDecimal2(0));
        this.setAttribute("data",0);

    }
})
$('.xuwance').click(function(){
	var bb=$(this).attr('data');
    if (bb == 0) {
        $(this).css({
            background: "url("+path+"/images/hongdui.png)",
            backgroundSize:'contain'
        })
        this.setAttribute("data",1);
		var count = $('#count'+$(this).attr('id')).val();
		var price = $('#price'+$(this).attr('id')).attr('data');
        var installprice = $('#installprice'+$(this).attr('id')).attr('data');
        var allinstallPrice = parseInt(count)*parseFloat(installprice);

        var zongPrice = $('#zonge').val();
		var allPrice=parseInt(count)*parseFloat(price)+parseFloat(zongPrice)+parseFloat(allinstallPrice);
		$('#zonge').val(toDecimal2(allPrice));
		$('.heqian').html("￥："+toDecimal2(allPrice));
    }else{
        $(this).css({
            background: "url("+path+"/images/raw_1503991404.png)",
            backgroundSize:'contain'
        })
	this.setAttribute("data",0);
        var count = $('#count'+$(this).attr('id')).val();
        var price = $('#price'+$(this).attr('id')).attr('data');
        var installprice = $('#installprice'+$(this).attr('id')).attr('data');
        var zongPrice = $('#zonge').val();
        var allPrice=parseFloat(zongPrice)-parseInt(count)*parseFloat(price);
        var allinstallPrice = parseInt(count)*parseFloat(installprice);
        allPrice = parseFloat(allPrice)-parseFloat(allinstallPrice);
        $('.heqian').html("￥："+toDecimal2(allPrice));
        $('#zonge').val(toDecimal2(allPrice));

    }

})
//制保留2位小数，如：2，会在2后面补上00.即2.00
var id="";

function toDecimal2(x) {
    var f = parseFloat(x);
    if (isNaN(f)) {
        return false;
    }
    var f = Math.round(x*100)/100;
    var s = f.toString();
    var rs = s.indexOf('.');
    if (rs < 0) {
        rs = s.length;
        s += '.';
    }
    while (s.length <= rs + 2) {
        s += '0';
    }
    return s;
}
function ObjData(key,value,key1,value1){

return "{\""+key+"\":"+value+",\""+key1+"\":"+value1+"}";

}
$('.tijiaoclick').click(function () {
    $.DialogByZ.Loading(path+'/images/loading.png');

    $.ajax({
        type:'post',
        url: $('#commentForm').attr('action'),
        data: $('#commentForm').serialize(),
        dataType:'json',
        success:function (result) {
            if(result.code==1){
                $.DialogByZ.Close();

                window.location.href="/orderUser/toPay";
            }else{
                $.DialogByZ.Alert({Title: "提交失败", Content:"商品存在您的未支付订单内",BtnL:"确定"});

            }
        }


    })

})

$('.dizis').click(function () {
    window.location.href="/orderUserAddress/addressList?orderType="+$('#orderType').val();

})
$('.adddizi').click(function () {
    window.location.href="/orderUserAddress/addAddress?type="+$("#type").val()+"";
})
var array="[";
$('#jiesuan').click(function () {
    $.DialogByZ.Loading(path+'/images/loading.png');
    $(".xuwance").each(function(index){

       if($(this).attr('data')==1){
        var key2="count";
        var value1=$('#count'+$(this).attr('id')).val();
        var key = "cartId";
        var value=$(this).attr('id');
        var s=ObjData(key,value,key2,value1);
           if(id==""){
               id=value;
           }else{
               id=id+","+value;
           }
        array=array+s+",";

    }
})
array = array.substr(0, array.length - 1);
array=array+']';

    $.ajax({
        url:'/orderCart/checkCart',
        data:{'orderCart':array},
        type:'post',
        dataType:'json',
        success:function (result) {
        if(result.code==1){
            $.DialogByZ.Close();
            $.DialogByZ.Alert({Title: "提交成功", Content:result.data ,BtnL:"确定",FunL:localHref});
        }
        else{
            $.DialogByZ.Close();
            $.DialogByZ.Alert({Title: "提交失败", Content:result.data ,BtnL:"确定"})
        }
        }
    })
});
function localHref(){
    window.location.href="/orderCart/toOrderCart?type=0&id="+id;
}


function arrayToJson(o) {
    var r = [];
    if (typeof o == "string") return "\"" + o.replace(/([\'\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";
    if (typeof o == "object") {
        if (!o.sort) {
            for (var i in o)
                r.push(i + ":" + arrayToJson(o[i]));
            if (!!document.all && !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)) {
                r.push("toString:" + o.toString.toString());
            }
            r = "{" + r.join() + "}";
        } else {
            for (var i = 0; i < o.length; i++) {
                r.push(arrayToJson(o[i]));
            }
            r = "[" + r.join() + "]";
        }
        return r;
    }
    return o.toString();
}
$('.addbaocun').click(function () {
    $.DialogByZ.Loading(path+'/images/loading.png');

    $.ajax({
        type:'post',
        url: $('#commentForm').attr('action'),
        data: $('#commentForm').serialize(),
        dataType:'json',
        beforeSend: function () {
            if ($('#username').val() == '') {
                alert("请输入收货人姓名");
                $.DialogByZ.Close();

                return false;
            }
            if ($('#phone').val() == '') {
                alert("请输入收货人手机号码");
                $.DialogByZ.Close();

                return false;
            }
            if ($('#address').val() == '') {
                alert("请输入收货人地址");
                $.DialogByZ.Close();

                return false;
            }
        },
        success:function (result) {
            if(result.code==1){
                $.DialogByZ.Close();
                $.DialogByZ.Alert({Title: "提交成功", Content:result.data ,BtnL:"确定",FunL:window.location.href="/orderUserAddress/addressList?orderType="+result.data});
            }
        }


    })

})
$('.dizxuanz').click(function () {
    var  id =($(this).attr('data'));
    $.ajax({
        type:'post',
        url: "/orderUserAddress/getAddress",
        data: {"id":id,"type":$("#type").val()},
        dataType:'json',
        success:function (result) {
            if(result.code==1){
                $.DialogByZ.Close();
                window.location.href="/orderCart/toOrderCart?type="+$("#type").val()+"&id="+result.data;
            }
        }


    })})



